package inititalization

import (
	"fmt"
	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"practice/config"
	"practice/internal/model/dao"
)

var (
	err error
)

func init() {
	ViperInit()
	MysqlInit()
}
func ViperInit() {
	viper.SetConfigFile("D:\\goWork\\practice\\applicational.yaml")
	err = viper.ReadInConfig()
	if err != nil {
		fmt.Println("获取失败")
		return
	} else {
		fmt.Println("获取成功")
	}
	err = viper.Unmarshal(&config.AppConfig)
	if err != nil {
		fmt.Println("解析失败")
		return
	} else {
		fmt.Println("解析成功")
	}
}
func MysqlInit() {
	app := config.AppConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", app.User, app.Password, app.Host, app.Port, app.Database)
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("mysql init false")
		return
	} else {
		fmt.Println("mysql init success")
	}
	config.DB.AutoMigrate(&dao.User{}, &dao.Travel{}, &dao.UserCommit{}, &dao.Friends{}, &dao.UserDetail{})
}
